package viewmodel;

import items.EntityItem;

import java.util.UUID;

import android.app.betmein.R;
import android.app.betmein.activity.CreateBetActivity;
import android.app.betmein.activity.EntityActivity;
import android.app.betmein.view.EntitiesView;
import android.app.betmein.view.ViewCreatorHelper;
import android.content.Context;
import android.framework.binding.BindableList;
import android.framework.binding.Binding;
import android.framework.binding.ItemList;
import android.framework.paging.BaseViewModel;
import android.framework.reflaction.annotation.OnClickListenerInjectionAnnotation;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import componentes.CommunicationManager;

public class EntitiesViewModel extends BaseViewModel<EntitiesView>{
	
	private ItemList<EntityItem> users;
	
	public EntitiesViewModel(Context ctx){
		super(ctx);
		users = new ItemList<EntityItem>(ctx, R.layout.item_entity, ViewCreatorHelper.entityListViewCreator());
		
	}

	public ItemList<EntityItem> getUsers() {
		return users;
	}

	public void setUsers(BindableList<EntityItem> users) {
		
		this.users.clear();
		for (EntityItem itemEntity : users) {
			this.users.add(itemEntity);
		}
		Binding.attached(users, getUsers());
		
	}

	@Override
	public void onInit() {
		BindableList<EntityItem> entities = CommunicationManager.getInstance().getEntities(UUID.randomUUID());
        setUsers(entities);
		getView().setEntitiesListAdapter(getUsers());
		getView().getEntitiesList().setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				startActivity(EntityActivity.class, parent.getItemAtPosition(position));
			}
		});
	}

	@OnClickListenerInjectionAnnotation(R.id.button1)
	public void onClick(){
		startActivity(CreateBetActivity.class);
	}
	
}
